package com.conversationboard.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.conversationboard.controller.debug.ServletDebugInfo;
import com.conversationboard.logger.Logger;
import com.conversationboard.model.Board;
import com.conversationboard.model.BoardAccess;
import com.conversationboard.model.Boards;
import com.conversationboard.model.Role;
import com.conversationboard.model.User;
import com.conversationboard.security.EncryptedKey;
import com.conversationboard.security.EncryptionException;

@WebServlet(name = "AdminUpdateBoardControllerServlet", urlPatterns = "/Admin/AdminUpdateBoardControllerServlet")
public class AdminUpdateBoardControllerServlet extends HttpServlet {

	private static final long serialVersionUID = -7190693755974493900L;


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		try {

			User user = User.get(request.getUserPrincipal().getName());

			if (!user.isSiteAdmin()) {
				request.setAttribute("message", "You need to be a Site Admin to edit boards.");

				RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
				dispatcher.forward(request, response);
				return;
			}

			String key = request.getParameter("key");
			EncryptedKey encryptedKey = new EncryptedKey();

			if (!encryptedKey.isValid(key, user.getLoginId())) {
				Logger.log(new Date(), "Part 1 of error: " + this.getClass().getName(), user);
				String postRequest = ServletDebugInfo.getRawRequest(request);
				Logger.log(new Date(), "Part 2 of error:\n\n" + postRequest, user);
				return;
			}

			if (!(user.getRole() == Role.SiteAdmin)) {
				request.setAttribute("message", "You must be a site administrator to update a board.");
				RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
				dispatcher.forward(request, response);
				return;
			}

			/* Get data from request */

			String name = request.getParameter("name");
			String description = request.getParameter("description");
			String popularBoardCheckBox = request.getParameter("popularCheckBox");
			String hiddenBoardCheckBox = request.getParameter("hiddenCheckBox");
			String requireRegistrationCheckBox = request.getParameter("checkbox4");
			String adminsAddUsersCheckBox = request.getParameter("checkbox6");
			String allowPollsCheckBox = request.getParameter("checkbox7");
			String logAdminActionsCheckBox = request.getParameter("checkbox8");
			String noIndexCheckBox = request.getParameter("noindex");
			String migrateUsersCheckBox = request.getParameter("migrateUsers");
			BoardAccess boardAccess = BoardAccess.valueOf(request.getParameter("boardAccess"));
			int boardId = Integer.parseInt(request.getParameter("boardId"));
			int sandboxDays = Integer.parseInt(request.getParameter("sandboxDays"));
			int numberOfNewPosts = Integer.parseInt(request.getParameter("numberOfNewPosts"));

			boolean popularBoard = false;

			if (popularBoardCheckBox != null && popularBoardCheckBox.equals("popular")) {
				popularBoard = true;
			}

			boolean hiddenBoard = false;

			if (hiddenBoardCheckBox != null && hiddenBoardCheckBox.equals("hidden")) {
				hiddenBoard = true;
			}

			boolean requireRegistration = false;

			if (requireRegistrationCheckBox != null && requireRegistrationCheckBox.equals("requireRegistration")) {
				requireRegistration = true;
			}

			boolean adminsAddUsers = false;

			if (adminsAddUsersCheckBox != null && adminsAddUsersCheckBox.equals("adminsAddUsers")) {
				adminsAddUsers = true;
			}

			boolean allowPolls = false;

			if (allowPollsCheckBox != null && allowPollsCheckBox.equals("allowPolls")) {
				allowPolls = true;
			}

			boolean noIndex = false;

			if (noIndexCheckBox != null && noIndexCheckBox.equals("true")) {
				noIndex = true;
			}

			boolean logAdminActions = false;

			if (logAdminActionsCheckBox != null && logAdminActionsCheckBox.equals("logAdminActions")) {
				logAdminActions = true;
			}

			boolean migrateUsers = false;

			if (migrateUsersCheckBox != null && migrateUsersCheckBox.equals("true")) {
				migrateUsers = true;
			}

			Board board = Boards.getBoard(boardId);
			board.update(user, name, description, popularBoard, boardAccess, migrateUsers, requireRegistration, hiddenBoard, adminsAddUsers, allowPolls, noIndex, logAdminActions, sandboxDays,
					numberOfNewPosts);

			request.setAttribute("message", "Board successfully updated.");

			RequestDispatcher dispatcher = request.getRequestDispatcher("/Pages/messagepage.jsp");
			dispatcher.forward(request, response);

		} catch (EncryptionException e) {
			throw new ServletException(e);
		} catch (SQLException e) {
			throw new ServletException(e);
		}

	}

}
